package com.jinke.api.common.util;

import com.alibaba.excel.util.StringUtils;
import com.jinke.api.modules.app.model.OrderParcel;
import com.jinke.api.modules.app.response.UserOrderSummaryVO;
import com.jinke.api.modules.app.util.OssUtil;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.util.FileCopyUtils;

import javax.annotation.Resource;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class FileUtil {
    public static MockMultipartFile listToMultipartFile(List<String> content) {
        try {
            // 创建临时文件
            File tempFile = File.createTempFile("temp", ".txt");
            tempFile.deleteOnExit();

            // 将列表内容写入文件
            try (FileOutputStream fos = new FileOutputStream(tempFile)) {
                for (String line : content) {
                    fos.write(line.getBytes(StandardCharsets.UTF_8));
                    fos.write('\n');
                }
            }

            // 将文件转换为 MultipartFile
            return new MockMultipartFile(tempFile.getName(), tempFile.getName(),
                    "text/plain", FileCopyUtils.copyToByteArray(tempFile));
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }


    @Resource
    private OssUtil ossUtil;

    public static MockMultipartFile toExcel(List<UserOrderSummaryVO> summaryVOS) {
        byte[] excelData = new byte[0];
        try {
            excelData = FileConverter.listToExcel(summaryVOS);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        if (excelData!= null) {
            return new MockMultipartFile("summary.xlsx", "summary.xlsx",
                    "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", excelData);
        } else {
            return null;
        }
    }



    public static MockMultipartFile listToMultipartFile1( List<UserOrderSummaryVO> summaryVOS) {
        try {


            // 创建临时文件
            File tempFile = File.createTempFile("temp", ".txt");
            tempFile.deleteOnExit();

            // 将列表内容写入文件
            try (FileOutputStream fos = new FileOutputStream(tempFile)) {
                for (UserOrderSummaryVO summaryVO : summaryVOS) {
                    String orderDate = summaryVO.getOrderDate();
                    List<OrderParcel> parcelByOrderIdList = summaryVO.getParcelByOrderIdList();

                    try {
                        fos.write(("日期："+orderDate).getBytes(StandardCharsets.UTF_8));
                        fos.write('\n');
                        for (OrderParcel parcel : parcelByOrderIdList) {
                            fos.write(parcel.getTrackingNumber().getBytes(StandardCharsets.UTF_8));

                            fos.write('\n');

                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }


            }

            // 将文件转换为 MultipartFile
            return new MockMultipartFile(tempFile.getName(), tempFile.getName(),
                    "text/plain", FileCopyUtils.copyToByteArray(tempFile));
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
}
